<?php


class Dashboard_Model extends Model
{
	
	
	public function __construct()
	{
		parent::__construct();
	}
    
    /**
     * ajax insert
     */
    public function xhrInsert()
    {
        $text = $_POST['text'];
        $stm = $this->db->prepare('insert into data (text) values (:text)');
        $stm->execute(array(':text' => $text));
        
        // json
        $text = array('id' => $this->db->lastInsertId(), 'text' => $text);
        echo json_encode($text);
        
        exit();
    }
    
    
    /**
     * ajax get list
     */
    public function xhrGetListings()
    {
        $stm = $this->db->prepare('select * from data');
        $stm->setFetchMode(PDO::FETCH_ASSOC);
        $stm->execute();
        $data = $stm->fetchAll();       

        // json
        echo json_encode($data);
        exit();
    }
    

    /**
     * ajax delete
     */
    public function xhrDelListing()
    {
        // @todo: input-filter
        $id = $_POST['id'];
        $stm = $this->db->prepare('select id from data where id = :id');
        $check = $stm->execute(array(':id' => $id));


        // if isset datarecord -> delete
        if($check) 
        {
            $stm = $this->db->prepare('delete from data where id = :id');
            $check = $stm->execute(array(':id' => $id));
        } else {
            // error
        }

    }
}